package com.none.web.controller.news;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.mvc.bind.annotation.FormModel;

import com.none.core.common.utils.JSONUtil;
import com.none.core.model.Pager;
import com.none.core.model.Result;
import com.none.web.common.SysConstant;
import com.none.web.model.TNews;
import com.none.web.model.TNewsImage;
import com.none.web.service.news.INewsService;

/**
 * 新闻管理Controller
 * 
 * @author Summer
 * 
 */
@Controller
@RequestMapping("news")
public class NewsController {

	@Resource(name = "newsService")
	INewsService newsServie;

	@Autowired
	private HttpServletRequest request;

	/**
	 * 添加新闻
	 * 
	 * @param news
	 * @param tags
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("addNews")
	public Object addNews(@FormModel("news") final TNews news, String tags,String toBePostedTime,String push,String pushcontent) throws Exception {
		String[] filter = new String[] { "TBeacons", "TNewses", "password", "lastTime", "tcodes", "approvalUser",
				"updateUser","lastTime" };

		String userId = request.getHeader("BasicAuthUsername");

		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "news.addSuccess", data);

		if (!newsServie.addNews(news, tags, userId,toBePostedTime,push,request,pushcontent)) {
			result = new Result(SysConstant.STATE_ERROR, "news.addError", data);
		}

		return JSONUtil.toJSON(result, filter);
	}

	/**
	 * 保存草稿
	 * 
	 * @param news
	 * @param tags
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveDraft")
	public Object saveDraft(@FormModel("news") final TNews news, String tags,String imageList_orderIndexs,String imageList_paths,String imageList_descs,String toBePostedTime,String push,String pushcontent) throws Exception {
		//image list
		List<TNewsImage> imageList = new ArrayList<TNewsImage>();
		TNewsImage image = null;
		if(imageList_paths!=null && !imageList_paths.equals("")){
			String[] imageList_pathArr = imageList_paths.split(",,");
			String[] imageList_descArr = imageList_descs.split(",,");
			String[] imageList_orderIndexArr = imageList_orderIndexs.split(",,");
			for (int i = 0; i < imageList_pathArr.length; i++) {
				image = new TNewsImage();
				try{
				image.setOrderIndex(Integer.parseInt(imageList_orderIndexArr[i]));
				}catch(Exception e){
					image.setOrderIndex(0);
				}
				image.setImagePath(imageList_pathArr[i]);
				image.setDescription(imageList_descArr[i]);
				imageList.add(image);
			}
		}
		news.setImageList(imageList);
		//
		String[] filter = new String[] { "TBeacons", "TNewses", "password", "lastTime", "tcodes", "approvalUser",
				"updateUser", "editTime", "approvalTime" };
		String userId = request.getHeader("BasicAuthUsername");
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "news.addSuccess", data);
        if(news!=null){
        	if("".equals(news.getNewsId())||news.getNewsId()==null){
        		result = new Result(SysConstant.STATE_SUCCESS, "news.saveDraftSuccess", data);
        	}
        }
		if (!newsServie.saveDraft(news, tags, userId,toBePostedTime,push,request,pushcontent)) {
			result = new Result(SysConstant.STATE_ERROR, "news.updateError", data);
		}
		return JSONUtil.toJSON(result, filter);
	}

	/**
	 * app新闻list
	 * 
	 * @param pager
	 * @param keyword
	 * @param tagNames
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@ResponseBody
	@RequestMapping("listNews")
	public Object listNews(@FormModel("pager") final Pager pager, String keyword, String tagNames,String type) throws Exception{
		String[] filter = new String[] { "TBeacons", "TNewses", "password", "tcodes","lastTime","activitys"};
		String userId = request.getHeader("BasicAuthUsername");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("news", newsServie.listNews(pager, keyword, tagNames,userId,type));
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "news.haveNews", data);
		return JSONUtil.toJSON(result, filter);
	}
	/**
	 * app新闻list
	 * 
	 * @param pager
	 * @param keyword
	 * @param tagNames
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@ResponseBody
	@RequestMapping("listNewsActivitys")
	public Object listNewsActivitys(@FormModel("pager") final Pager pager, String keyword, String tagNames,String type,String userId) throws Exception{
		String[] filter = new String[] { "TBeacons", "TNewses", "password", "tcodes","lastTime","activitys","appUsers"};
		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}
		Map<String, Object> data = new HashMap<String, Object>();
		pager.setPageSize(20);
		data.put("news", newsServie.listNewsActivitys(pager, keyword, tagNames,type,userId));
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "news.haveNews", data);
		return JSONUtil.toJSON(result, filter);
	}
	/**
	 * 管理后台搜索 新闻list
	 * 
	 * @param pager
	 * @param keyword
	 * @param tagNames
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@ResponseBody
	@RequestMapping("searchNews")
	public Object searchNews(@FormModel("pager") final Pager pager, String keyword, String tagName, String fromDate,
			String toDate) throws Exception {
		String userId = request.getHeader("BasicAuthUsername");
		String[] filter = new String[] { "TBeacons", "TNewses", "password", "lastTime", "tcodes","lastTime","activitys"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("news", newsServie.searchNews(pager, keyword, tagName, fromDate, toDate, userId));
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "news.haveNews", data);
		return JSONUtil.toJSON(result, filter);
	}

	/**
	 * 需要审批的新闻列表
	 * 
	 * @param pager
	 * @param branch
	 * @return
	 */
	@ResponseBody
	@RequestMapping("listPeding")
	public Object listPeding(@FormModel("pager") final Pager pager) throws Exception {
		String[] filter = new String[] { "TBeacons", "TNewses", "password", "lastTime", "tcodes","lastTime","activitys"};
		String userId = request.getHeader("BasicAuthUsername");

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("news", newsServie.listPeding(pager, userId));   
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "news.haveNews", data);
		return JSONUtil.toJSON(result, filter);
	}

	/**
	 * 更新新闻
	 * 
	 * @param news
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("editNews")
	public Object editNews(@FormModel("news") final TNews news, String tags,String toBePostedTime,String push,String pushcontent) throws Exception {
		String[] filter = new String[] { "TBeacons", "TNewses", "password", "lastTime", "tcodes","activitys"};

		String userId = request.getHeader("BasicAuthUsername");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("news", newsServie.editNews(news, tags, userId,toBePostedTime,push,request,pushcontent));
		Result result = new Result(SysConstant.STATE_SUCCESS, "news.updateSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}

	/**
	 * 删除新闻
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("delNews")
	public Object delNews(String newsId) throws Exception {
		String userId = request.getHeader("BasicAuthUsername");
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "news.delSuccess", data);
		if (!newsServie.delNews(newsId, userId)) {
			result = new Result(SysConstant.STATE_ERROR, "news.delError", data);
		}
		return JSONUtil.toJSON(result);
	}

	/**
	 * 关联新闻和灯箱
	 * 
	 * @param newsId
	 * @param beaconId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("assignToBeacon")
	public Object assignToBeacon(String newsId, String beaconId) throws Exception {
		String[] filter = new String[] { "TBeacons", "TNewses", "password", "lastTime", "tcodes","activitys"};


		Map<String, Object> data = new HashMap<String, Object>();
		data.put("beacon", newsServie.assignToBeacon(newsId, beaconId));

		Result result = new Result(SysConstant.STATE_SUCCESS, "news.assignSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}

	/**
	 * 审核通过
	 * 
	 * @param newsId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("auditPublished")
	public Object auditPublished(@FormModel("news") final TNews news, String tags,String toBePostedTime,String push,String pushcontent) throws Exception {

		String[] filter = new String[] { "TBeacons", "TNewses", "password", "lastTime", "tcodes","activitys"};

		String userId = request.getHeader("BasicAuthUsername");

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("news", newsServie.auditPublished(news, tags, userId,toBePostedTime,push,request,pushcontent));

		Result result = new Result(SysConstant.STATE_SUCCESS, "news.updateSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}

	/**
	 * 审核拒绝
	 * 
	 * @param newsId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("auditReject")
	public Object auditReject(String newsId,String push,String pushcontent) throws Exception {
		String[] filter = new String[] { "TBeacons", "TNewses", "password", "lastTime", "tcodes","activitys"};

		String userId = request.getHeader("BasicAuthUsername");

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("news", newsServie.auditNewsStatus(newsId, "Rejected", userId,push,request,pushcontent));

		Result result = new Result(SysConstant.STATE_SUCCESS, "news.RejectSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	/**
	 * 获取新闻详细信息
	 * 
	 * @param newsId
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getDetialInfo")
	public Object getNewsDetialInfo(String newsId,String type,String appOrPCFlag,String userId) throws Exception {
		String[] filter = new String[] { "TBeacons", "TNewses", "password", "lastTime", "tcodes","activitys"};

		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}

		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "news.haveNews", data);

		data.put("news", newsServie.getNewsDetialInfo(newsId, type, userId,appOrPCFlag));
		return JSONUtil.toJSON(result, filter);
	}
}
